package com.hunliji.monitor.web.strategy.interception.handler;

import com.hunliji.common.exception.WarnException;
import com.hunliji.monitor.share.enums.feishu.InterceptEventTypeEnum;
import com.hunliji.monitor.web.facade.impl.FeiShuFacadeImpl;
import com.hunliji.monitor.web.strategy.interception.param.FeishuInterceptionContext;
import com.lark.project.core.Config;
import com.lark.project.core.token.GlobalTokenManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;

import javax.annotation.Resource;

@Slf4j
public abstract class AbstractFeishuInterceptionHandler implements FeishuInterceptionHandler, InitializingBean {

    @Resource
    private Config config;

    /**
     * 获取access_token
     */
    protected String getAccessToken(){
        String accessToken;
        try {
            accessToken = GlobalTokenManager.getTokenManager().getAccessTokenThenCache(config);
            log.info("获取token成功，token={}", accessToken);
            return accessToken;
        } catch (Exception e) {
            throw new WarnException("获取accessToken失败");
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        FeiShuFacadeImpl.register(getEventType(), this);
    }
}
